<?php
$title = 'Entrenamiento: ' . $trainingData->training_name;
$this->headTitle($title);
$problemForm->prepare();
$groupForm->prepare();
$host = $_SERVER['HTTP_HOST'];

function getFormattedDate($date) {
    $phpdate = strtotime($date);
    return date('Y-m-d', $phpdate);
}

function getFormattedTime($time) {
    $phpdate = strtotime($time);
    return date('H:i', $phpdate);
}
?>

<link rel="stylesheet" href="/css/jquery-ui-1.10.3.css">
<script src="/js/jquery-1.9.1.js"></script>
<script src="/js/jquery-ui-1.10.3.js"></script>
<script>
    $(function() {
        $("#problem_id").autocomplete({
            source: function(request, response) {
                console.log(request);
                $.ajax({
                    url: "http://" + "<?php echo $host ?>" + "/training/autocomplete1/term/" + request.term,
                    dataType: "json",
                    data: {
                    },
                    success: function(data) {
                        console.log(data);
                        response($.map(data.problems, function(item) {
                            return {
                                label: item.problem_id + " - " + item.problem_name,
                                value: item.problem_id + " - " + item.problem_name
                            }
                        }));
                    }
                });
            },
            minLength: 1,
            select: function(event, ui) {
                (ui.item ? $("#project_id").val(ui.item.problem_id) : "");
            },
            open: function() {
                $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
            },
            close: function() {
                $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
            }
        });
        
        $("#group_id").autocomplete({
            source: function(request, response) {
                console.log(request);
                $.ajax({
                    url: "http://" + "<?php echo $host ?>" + "/training/autocomplete2/term/" + request.term,
                    dataType: "json",
                    data: {
                    },
                    success: function(data) {
                        console.log(data);
                        response($.map(data.groups, function(item) {
                            return {
                                label: item.group_id + " - " + item.group_name,
                                value: item.group_id + " - " + item.group_name
                            }
                        }));
                    }
                });
            },
            minLength: 1,
            select: function(event, ui) {
                (ui.item ? $("#group_id").val(ui.item.group_id) : "");
            },
            open: function() {
                $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
            },
            close: function() {
                $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
            }
        });
    });
</script>
<div class="content">
    <h1><?php echo $this->escapeHtml($title); ?></h1>
    <table style="margin-left: 15px">	      
        <tr>
            <td valign="mid"><label>Inicio:</label></td>
            <td><?php
echo $this->escapeHtml(getFormattedDate($trainingData->start_date) . ' ' .
        getFormattedTime($trainingData->start_time));
?></td>
        </tr>
        <tr>
            <td valign="mid"><label>Fin:</label></td>
            <td><?php
                echo $this->escapeHtml(getFormattedDate($trainingData->end_date) . ' ' .
                        getFormattedTime($trainingData->end_time));
?></td>
        </tr>
    </table>
    <?php
    if ($isOwner) {
        echo $this->form()->openTag($problemForm);
        ?>
        <h2>Agregar problema</h2>
        <table>	      
            <tr>
                <td><?php echo $this->formInput($problemForm->get('problem_id')); ?></td>
                <td><?php echo $this->formSubmit($problemForm->get('addproblem')); ?></td>
                <td><?php echo $this->formSubmit($problemForm->get('removeproblem')); ?></td>
                <td><?php echo $this->formElementErrors($problemForm->get('problem_id')); ?></td>
            </tr>
        </table>
        <?php
        echo $this->form()->closeTag();
        echo $this->form()->openTag($groupForm);
        ?>
        <h2>Agregar grupo</h2>
        <table>
            <tr>
                <td><?php echo $this->formInput($groupForm->get('group_id')); ?></td>
                <td><?php echo $this->formSubmit($groupForm->get('addgroup')); ?></td>
                <td><?php echo $this->formSubmit($groupForm->get('removegroup')); ?></td>
                <td><?php echo $this->formElementErrors($groupForm->get('group_id')); ?></td>
            </tr>
        </table>
        <?php
        echo $this->form()->closeTag();
    }
    ?>
    <center>
        <h2>Problemas Disponibles</h2>
        <?php
        if (isset($trainingProblems)) {
            if (count($trainingProblems) > 0) {
                ?>
                <div style="margin-top: 20px;">
                    <table class="table-bordered"  style="width: 50%">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Problema</th>

                            </tr>
                        </thead>
                        <?php foreach ($trainingProblems as $problem) : ?>
                            <tr>
                                <td><?php echo $this->escapeHtml($problem->problem_id); ?></td>
                                <td>
                                    <a href="<?php echo $this->url('problem', array('action' => 'display', 'id' => $problem->problem_id)); ?>"><?php echo $this->escapeHtml($problem->problem_name); ?></a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </table>
                </div>

                <?php
            } else {
                echo "No existen problemas para este entrenamiento.";
            }
        } else {
            echo "El entrenamiento aun no esta habilitado.</br>" .
            "Los problemas estaran disponibles cuando el entrenamiento inicie.";
        }
        ?>

        <h2>Grupos</h2>
        <?php
        if (count($trainingGroups) > 0) {
            ?>
            <div style="margin-top: 20px;">
                <table class="table-bordered"  style="width: 50%">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Grupo</th>
                        </tr>
                    </thead>
                    <?php foreach ($trainingGroups as $group) : ?>
                        <tr>
                            <td><?php echo $this->escapeHtml($group->group_id); ?></td>
                            <td>
                                <a href="<?php echo $this->url('group', array('action' => 'edit', 'id' => $group->group_id)); ?>"><?php echo $this->escapeHtml($group->group_name); ?></a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </table>
            </div>
            <?php
        }else {
            echo "No existen grupos para este entrenamiento.";
        }
        ?>
    </center>
    <br/>
    <a style="margin-left: 25px" 
       href="<?php echo $this->url('training', array('action' => 'index',)); ?>" 
       class="button">Volver</a>
</div>